// popup.js
document.addEventListener('DOMContentLoaded', function() {
    // 设置复选框的初始状态
    const ballVisible = localStorage.getItem('floatingButtonVisible') === 'true';
    document.getElementById('toggleBall').checked = ballVisible;

    // 监听复选框的变化事件
    document.getElementById('toggleBall').addEventListener('change', function(event) {
        const action = event.target.checked ? 'show' : 'hide';
        sendMessageToActiveTab(action);
        // 更新本地存储中悬浮球的状态
        localStorage.setItem('floatingButtonVisible', action === 'show' ? 'true' : 'false');
    });

    // 监听“鉴别”按钮的点击事件
    document.getElementById('identify').addEventListener('click', function() {
        // 发送消息到内容脚本，调用endFun函数
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            if (tabs[0]) {
                chrome.tabs.sendMessage(tabs[0].id, {action: 'endFun'});
            }
        });
    });
});

function sendMessageToActiveTab(action) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        if (tabs[0]) {
            chrome.tabs.sendMessage(tabs[0].id, {action: action});
        }
    });
}